spi_main.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. #include <linux/module.h>
  2. #include <linux/kernel.h>
  3. #include <linux/init.h>
  4. #include <linux/cdev.h>
  5. #include <linux/fs.h>
  6. #include <linux/errno.h>
  7. #include <asm/current.h>
  8. #include <linux/sched.h>
  9. #include <linux/device.h>
  10. #include <linux/err.h>
  11. #include "asm/uaccess.h"
  12. #include <linux/delay.h>
  13. #include "spi.h"
  14. #include "./stm32f429_spi.h"
  15. static int major = MAJOR_SPI;
  16. static int minor = 0;
  17. static dev_t devno;
  18. static struct cdev *spi_cdev = NULL;
  19. static int count = 6;
  20. static int ret=0;
  21. #define DEVNAME "spi"
  22. #define SPIT_FLAG_TIMEOUT ((uint32_t)0x1000)
  23. #define SPIT_LONG_TIMEOUT ((uint32_t)(10 * I2CT_FLAG_TIMEOUT))
  24. int iic_read_byte(void);
  25. static int spi_open(struct inode *inode, struct file *filep);
  26. static int spi_close(struct inode *inode, struct file *filep);
  27. static ssize_t spi_read(struct file *filep, char __user *buf, size_t size, loff_t *offset);
  28. static ssize_t spi_write(struct file *filep, const char __user *buf, size_t size, loff_t *offset);
  29. static int spi_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg);
  30. static struct file_operations spi_ops =
  31. {
  32. .owner = THIS_MODULE,
  33. .open = spi_open,
  34. .release = spi_close,
  35. .ioctl = spi_ioctl,
  36. .read = spi_read,
  37. .write = spi_write,
  38. };
  39. SPI_DEV_T spi[] = {{.bus = SPI1,},{.bus = SPI2,},{.bus = SPI3,},{.bus = SPI4,},{.bus = SPI5,},{.bus = SPI6,}};
  40. static int spi_open(struct inode *inode, struct file *filep)
  41. {
  42. int imajor,iminor;
  43. imajor = MAJOR(inode->i_rdev);
  44. iminor = MINOR(inode->i_rdev);
  45. if(imajor != major)
  46. {
  47. printk("Error: %s %d spi major fail! %d\n",__FILE__,__LINE__,imajor);
  48. return -1;
  49. }
  50. // printk("---> open major=%d, minor=%d\n",imajor,iminor);
  51. switch(iminor)
  52. {
  53. case 0:
  54. /* Enable SPI peripheral */
  55. SPI_Cmd(SPI1,ENABLE);
  56. break;
  57. case 1:
  58. /* Enable I2C peripheral */
  59. SPI_Cmd(SPI2,ENABLE);
  60. break;
  61. case 2:
  62. /* Enable I2C peripheral */
  63. SPI_Cmd(SPI3,ENABLE);
  64. break;
  65. case 3:
  66. /* Enable I2C peripheral */
  67. SPI_Cmd(SPI4,ENABLE);
  68. break;
  69. case 4:
  70. /* Enable I2C peripheral */
  71. SPI_Cmd(SPI5,ENABLE);
  72. break;
  73. case 5:
  74. /* Enable I2C peripheral */
  75. SPI_Cmd(SPI6,ENABLE);
  76. break;
  77. default:
  78. printk("Error:%s %d Invalid minor %d",__FILE__,__LINE__,iminor);
  79. return -1;
  80. }
  81. return 0;
  82. }
  83. static int spi_close(struct inode *inode, struct file *filep)
  84. {
  85. int imajor,iminor;
  86. imajor = MAJOR(inode->i_rdev);
  87. iminor = MINOR(inode->i_rdev);
  88. if(imajor != major)
  89. {
  90. printk("Error: %s %d spi major fail! %d\n",__FILE__,__LINE__,imajor);
  91. return -1;
  92. }
  93. switch(iminor)
  94. {
  95. case 0:
  96. /* Disable SPI peripheral */
  97. SPI_Cmd(SPI1,DISABLE);
  98. break;
  99. case 1:
  100. /* Disable I2C peripheral */
  101. SPI_Cmd(SPI2,DISABLE);
  102. break;
  103. case 2:
  104. /* Disable SPI peripheral */
  105. SPI_Cmd(SPI3,DISABLE);
  106. break;
  107. case 3:
  108. /* Disable SPI peripheral */
  109. SPI_Cmd(SPI4,DISABLE);
  110. break;
  111. case 4:
  112. /* Disable SPI peripheral */
  113. SPI_Cmd(SPI5,DISABLE);
  114. break;
  115. case 5:
  116. /* Disable SPI peripheral */
  117. SPI_Cmd(SPI6,DISABLE);
  118. break;
  119. default:
  120. printk("Error:%s %d Invalid minor %d",__FILE__,__LINE__,iminor);
  121. return -1;
  122. }
  123. return 0;
  124. }
  125. static int spi_ioctl(struct inode *inode, struct file *filep, unsigned int cmd, unsigned long arg)
  126. {
  127. SPI_DEV_T spi_dev;
  128. int read_data=0;
  129. ret = 0;
  130. if ( copy_from_user(&spi_dev, (void*)arg, sizeof(SPI_DEV_T)) != 0)
  131. {
  132. printk("---> copy form user space fail!\n");
  133. return -1;
  134. }
  135. switch(cmd)
  136. {
  137. // case SET_I2C_ADDR:
  138. // printk("---> set i2c addr.\n");
  139. // i2c_dev.bus->OAR1 = i2c_dev.ownAddr;
  140. break;
  141. // case GENERATE_STOP:
  142. // I2C_GenerateSTOP(i2c_dev.bus, ENABLE);
  143. break;
  144. default:
  145. printk("---> Invalid action\n");
  146. return -1;
  147. break;
  148. }
  149. return ret;
  150. }
  151. static ssize_t spi_read(struct file *filep, char __user *buf, size_t size, loff_t *offset)
  152. {
  153. return 0;
  154. }
  155. static ssize_t spi_write(struct file *filep, const char __user *buf, size_t size, loff_t *offset)
  156. {
  157. return 0;
  158. }
  159. void spi_config_init()
  160. {
  161. GPIO_InitTypeDef spi_gpio;
  162. SPI_InitTypeDef spi;
  163. //gpio init
  164. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);
  165. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI5,ENABLE);
  166. spi_gpio.GPIO_OType=GPIO_OType_PP;
  167. spi_gpio.GPIO_PuPd=GPIO_PuPd_UP;
  168. spi_gpio.GPIO_Speed=GPIO_Speed_50MHz;
  169. spi_gpio.GPIO_Mode=GPIO_Mode_AF;
  170. spi_gpio.GPIO_Pin=GPIO_Pin_7;
  171. GPIO_Init(GPIOF, &spi_gpio);
  172. GPIO_PinAFConfig(GPIOF, GPIO_PinSource7, GPIO_AF_SPI5);
  173. spi_gpio.GPIO_Pin=GPIO_Pin_8;
  174. GPIO_Init(GPIOF, &spi_gpio);
  175. GPIO_PinAFConfig(GPIOF, GPIO_PinSource8, GPIO_AF_SPI5);
  176. spi_gpio.GPIO_Pin=GPIO_Pin_9;
  177. GPIO_Init(GPIOF, &spi_gpio);
  178. GPIO_PinAFConfig(GPIOF, GPIO_PinSource9, GPIO_AF_SPI5);
  179. spi_gpio.GPIO_Mode=GPIO_Mode_OUT;
  180. spi_gpio.GPIO_Pin=GPIO_Pin_6;
  181. GPIO_Init(GPIOF, &spi_gpio);
  182. GPIO_SetBits(GPIOF,GPIO_Pin_6);
  183. //spi config init
  184. spi.SPI_Direction=SPI_Direction_2Lines_FullDuplex;
  185. spi.SPI_Mode=SPI_Mode_Master;
  186. spi.SPI_DataSize=SPI_DataSize_8b;
  187. spi.SPI_CPOL=SPI_CPOL_High;
  188. spi.SPI_CPHA=SPI_CPHA_2Edge;
  189. spi.SPI_NSS=SPI_NSS_Soft;
  190. spi.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_2;
  191. spi.SPI_FirstBit=SPI_FirstBit_MSB;
  192. spi.SPI_CRCPolynomial=0;
  193. SPI_Init(SPI5,&spi);
  194. SPI_Cmd(SPI5,ENABLE);
  195. }
  196. unsigned char spi_byte_read(unsigned char addr)
  197. {
  198. while (SPI_I2S_GetFlagStatus(SPI5, SPI_I2S_FLAG_TXE) == RESET);
  199. SPI_I2S_SendData(SPI5,addr);
  200. while (SPI_I2S_GetFlagStatus(SPI5, SPI_I2S_FLAG_RXNE) == RESET);
  201. return SPI_I2S_ReceiveData(SPI5);
  202. }
  203. static int __init spi_init(void)
  204. {
  205. int ret;
  206. int i;
  207. int tem=0;
  208. unsigned int pclk1;
  209. uint8_t frequence;
  210. spi_cdev = cdev_alloc();
  211. if(spi_cdev == NULL){
  212. return -ENOMEM;
  213. }
  214. cdev_init(spi_cdev,&spi_ops);
  215. devno = MKDEV(major,minor);
  216. ret = register_chrdev_region(devno, count, DEVNAME);
  217. if(ret){
  218. goto ERR_STEP;
  219. }
  220. ret = cdev_add(spi_cdev, devno, count);
  221. if(ret){
  222. goto ERR_STEP1;
  223. }
  224. spi_config_init();
  225. //Sm mode
  226. SystemCoreClockUpdate();
  227. pclk1 = SystemCoreClock >> APBPrescTable[(RCC->CFGR & RCC_CFGR_PPRE1)>> RCC_CFGR_PPRE1_Pos];
  228. printk("---> SystemCoreClock=%d, pclk1=%d\n",SystemCoreClock, pclk1);
  229. frequence = pclk1/1000000;
  230. if(frequence > 42)
  231. {
  232. frequence = 42;
  233. }
  234. if(frequence < 2)
  235. {
  236. frequence = 2;
  237. }
  238. for(i=0;i<6;i++)
  239. {
  240. // spi[i].bus->CR2 = frequence; //the max frequence is 42MHZ, min frequence is 2MHZ
  241. // printk("---> spi%d ,bus:%#x, CR2:%d, frequence: %d\n", i,spi[i].bus, spi[i].bus->CR2, frequence);
  242. // spi[i].bus->CCR = (frequence<<1) & 0x0FFF; //100K, standard mode
  243. // spi[i].bus->TRISE = frequence + 1;
  244. // printk("---> CCR:%#x, TRISE:%#x\n",spi[i].bus->CCR, spi[i].bus->TRISE);
  245. }
  246. return 0;
  247. ERR_STEP1:
  248. unregister_chrdev_region(devno,count);
  249. ERR_STEP:
  250. cdev_del(spi_cdev);
  251. return ret;
  252. }
  253. static void __exit spi_exit(void)
  254. {
  255. unregister_chrdev_region(MKDEV(major,minor),count);
  256. cdev_del(spi_cdev);
  257. }
  258. module_init(spi_init);
  259. module_exit(spi_exit);
  260. MODULE_LICENSE("GPL");
  261. MODULE_AUTHOR("zhouchao");
  262. MODULE_DESCRIPTION("this is spi module");